<?php
header('Content-Type: text/html');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';

$myPdo = DbUtils::createPdoInst();
$action = MiscUtils::getParam('action', '');
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'remove':
      remove($myPdo);
      break;
    case 'update':
      update($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function remove($myPdo) {
  $id = MiscUtils::getParam('id', '');
  $from = MiscUtils::getParam('from', '');
  $cond_vals = new stdClass();
  $cond_vals->c = 't.id = :v1';
  $cond_vals->v = array(':v1' => $id);
  $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_entry_pic', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmps) > 0) {
    $obj = $tmps[0];
    DbUtils::delete($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_entry_pic', $obj);
    $obj->path = iconv('UTF-8', 'GB2312', $obj->path);
    if (file_exists($obj->path)) {
      @unlink($obj->path);
    }
  }
  echo "
  <script>
  parent.location.href = '$from';
  </script>
  ";
}

function update($myPdo) {
  $id = MiscUtils::getParam('id', '');
  $address = MiscUtils::getParam('address', '');
  $url = MiscUtils::getParam('url', '');
  $obj = new stdClass();
  $obj->id = $id;
  $obj->address = $address;
  $obj->is_confirmed = 1;
  DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'comp_entry', $obj);
  echo "
  <script>
  parent.location.href = '$url';
  </script>
  ";
}
?>